package tt.lab.pmbok.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import tt.lab.pmbok.model.Artifact;
import tt.lab.pmbok.model.ArtifactTemplate;

public class ArtifactDaoHibernate  implements ArtifactDao {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	private Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	

	public List<Artifact> loadById(int id) {
		Query query;
		String hql;
		if (id > 0) {
			hql = "from "+Artifact.class.getName()+" where id = :id";
			System.out.println(hql);
			query = getSession().createQuery(hql);
			query.setInteger("id", id);
		} else {
			hql = "from "+Artifact.class.getName();
			System.out.println(hql);
			query = getSession().createQuery(hql);
		}

		List<Artifact> list = (List<Artifact>) query.list();
		return list;
	}


	public List<ArtifactTemplate> loadTemplateById(int id) {
		Query query;
		String hql;
		if (id > 0) {
			hql = "from "+ArtifactTemplate.class.getName()+" where id = :ida";
//			System.out.println(hql);
			query = getSession().createQuery(hql);
			query.setInteger("ida", id);
		} else {
			hql = "from "+ArtifactTemplate.class.getName();
//			System.out.println(hql);
			query = getSession().createQuery(hql);
		}

		List<ArtifactTemplate> list = (List<ArtifactTemplate>) query.list();
		return list;
	}


}
